import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue')
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/Login.vue')
    }
  ]
})

// 导航守卫、路由守卫、路由拦截
router.beforeEach((to, from, next) => {
  // localStorage.clear()
  let token = localStorage.getItem("token");
  if (token || to.path === '/login' || to.path === '/register') {
    next();
    // console.log("to:",to.path);
  } else {
    console.log("token不存在，正在前往登录")
    next('/login');
  }
})

export default router
